package com.shelpe.services.tenant.configuration;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.shelpe.services.tenant.mongomodel.SimbadTask;


@Configuration
@EnableScheduling
@EnableBatchProcessing
public class JobSchedulerConfiguration {

	private static final Logger logger = LoggerFactory.getLogger(JobSchedulerConfiguration.class);
	
	@Autowired
	private JobLauncher jobLauncher;
	 
	@Autowired 
	private JobBuilderFactory jobs;
 
	@Autowired
	private StepBuilderFactory steps;
	
	@Autowired 
	@Qualifier("syncData") 
	private Job syncData;
	
	private JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(); 
	
	
	@Scheduled(cron="0 */15 * * * ?") 
	public void schedule(){ 
		
		Date scheduleTime = new Date(); 
		
		JobParameters jobParams = jobParametersBuilder.addDate("scheduleTime", scheduleTime)
					.toJobParameters();
			
		try {
			
			logger.info(">>>>>>>>>>>>>>>>>schedule {}", scheduleTime);
			jobLauncher.run(syncData, jobParams);
			
		} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
				| JobParametersInvalidException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	 }
	
	
	 @Bean  
	 public Job syncData(@Qualifier("taskStep") Step step) {
	    
		 return jobs.get("taskScheduler")
						.start(step)
						.build();	 
	 }
	 
	 @Bean 
	 public Step taskStep(@Qualifier("taskReader") ItemReader<SimbadTask> reader,
			 @Qualifier("taskProcessor") ItemProcessor<SimbadTask, SimbadTask> processor,
			 @Qualifier("taskWriter") ItemWriter<SimbadTask> writer){
 
		 return steps.get("taskStep")
						.<SimbadTask, SimbadTask> chunk(20)
						.reader(reader)
						.processor(processor)
						.writer(writer)
						.build();
	 }
	 
	 
}
